#define _CRT_SECURE_NO_WARNINGS 1
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeElements(struct ListNode* head, int val)
{
    if (head == NULL)
        return NULL;

    while ((head != NULL) && (head->val == val))
    {
        struct ListNode* tmp = head;
        head = head->next;
        free(tmp);
    }

    if (head == NULL)
        return NULL;

    struct ListNode* cur = head->next;
    while (cur != NULL)
    {
        if (cur->val == val)
        {
            struct ListNode* prev = head;
            while (prev->next->val != val)
            {
                prev = prev->next;
            }
            prev->next = cur->next;
        }
        cur = cur->next;
    }

    return head;

}